import { sys } from 'cc';
import { SDKManager } from './SDKManager';
export class mRect {
    width: number;
    height: number;
    left: number;
    top: number;
    bottom: number;
    right: number

}
export const utils = {
    /**
     * 判断正方体是否相交
     * @param x1 box1.x
     * @param y1 box1.y
     * @param x2 box2.x
     * @param y2 box2.y
     * @param w width
     * @param h hight
     * @returns true flase
     */
    isBoxOverlap: function (x1: number, y1: number, x2: number, y2: number, w: number, h: number) {
        var horizontalOverlap = x1 + w > x2 && x2 + w > x1;
        var verticalOverlap = y1 + h > y2 && y2 + h > y1;
        return horizontalOverlap && verticalOverlap;
    },

    /**
     * 返回指定长度的指定数字并打乱顺序
     * @param len 生产的数组长度，需要刚好被3整除,如果不能整除返回空数组
     * @param n 生产的数组小于n,不包含的数字范围 例如：5  （0-4）
     * @returns 返回数组
     */
    getArrayVal: function (len: number, n: number) {
        let count: number = Math.floor(Math.floor(len / 3))//生产三个相同数组的数量 1，1，1
        if (count != len / 3) return []
        let arrayVal: any[] = []
        for (let i: number = 0; i < count; i++) {
            let num: number = Math.floor(Math.random() * n)
            arrayVal.push(...[num, num, num])
        }

        return this.getShuffledArray(arrayVal)
    },


    /**
     * 获取打乱数组
     * @param arr 要打乱顺序的数组
     * @returns 数组
     */
    getShuffledArray: function (arr: any[]) {
        let n = arr.length,
            random;
        while (0 != n) {
            random = (Math.random() * n--) >>> 0; // 无符号右移位运算符向下取整
            [arr[n], arr[random]] = [arr[random], arr[n]] // ES6的结构赋值实现变量互换
        }
        return arr;
    },

    /**
     * 从右向左查询符合条件的第一条记录
     * @param arr 查询来源对象数组
     * @param a 要查询的对象
     * @returns 返回对象在当前数组的位置，-1无符合条件的记录
     */
    getLastIndex: function (arr: any[], a: any) {
        let idx: number = -1
        for (var i: number = arr.length - 1; i >= 0; i--) {
            if (-1 == idx && (arr[i].v == a.v)) return idx = i;
        }
        return idx
    },


    call(name, data) {
        return;
        let url = 'https://xxx.com'
        if (sys.platform === sys.Platform.WECHAT_GAME) {
            return new Promise((resolve, reject) => {
                SDKManager.platform().request({
                    url,
                    method: 'POST',
                    headers: { 'content-type': 'application/json' },
                    dataType: 'json',
                    form: true,
                    data: { name, data },
                    success: res => {
                        resolve(res?.data?.result);
                    },
                    fail: err => {
                        reject(err);
                    }
                })

            });
        } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) {
            return new Promise((resolve, reject) => {
                SDKManager.platform().request({
                    url,
                    method: 'POST',
                    header: { 'Content-type': 'application/json' },
                    dataType: 'json',
                    data: { name, data },
                    success: res => {
                        resolve(res?.data?.result);
                    },
                    fail: err => {
                        reject(err);
                    }
                })
            });
        } else {
            return new Promise((resolve, reject) => {
                fetch(url, {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    body: JSON.stringify({ name, data }),
                }).then(response => {
                    if (response.ok) return response.json();
                    throw new Error('Network response was not ok.');
                }).then(data => {
                    return resolve(data?.result);
                }).catch(err => {
                    return reject(err);
                });
            });
        }
    },


}